home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
prolog
/
modprolg
/
mod-prol.lha
/
Prolog
/
Examples
/
search.mod
< prev
next >
Wrap
Text File
|
1992-03-12
|
2KB
|
71 lines
% Taken from Bratko.
signature searchsig =
sig
pred solve/2.
end.
signature problemsig =
sig
pred s/2 and goal/1.
end.
functor dfs(p/problemsig)/searchsig =
struct
structure x = p.
solve(Node,Solution) :-
df([],Node,Solution).
df(Path,Node,[Node|Path]) :-
x:goal(Node).
df(Path,Node,Sol) :-
x:s(Node,Node1),
not member(Node1,Path), % No Cycles!
df([Node|Path],Node1,Sol).
end.
functor bfs(p/problemsig)/searchsig =
struct
structure x = p.
solve(Node,Solution) :-
bf([[Node]],Solution).
bf([[Node|Path]|_],[Node|Path]) :-
x:goal(Node).
bf([[N|Path]|Paths],Solution) :-
bagof([M,N|Path],
(x:s(N,M),not member(M,[N|Path])),
Newpaths), % Newpaths = acyclic extensions of [N|Path]
append(Paths,Newpaths,Paths1), !,
bf(Paths1,Solution);
bf(Paths,Solution). % Case that N has no successors.
end.
structure eightqueens =
struct
goal([_,_,_,_,_,_,_,_]).
s(Queens,[Queen|Queens]) :-
member(Queen,[1,2,3,4,5,6,7,8]),
not member(Queen, Queens),
safe([Queen|Queens]).
safe([]).
safe([Queen|Others]) :-
safe(Others),
noattack(Queen,Others,1).
noattack(_,[],_).
noattack(Y,[Y1|Ylist],Xdist) :-
Y1 - Y =\= Xdist,
Y - Y1 =\= Xdist,
Dist1 is Xdist + 1,
noattack(Y, Ylist, Dist1).
end.
structure eightbfs = bfs(eightqueens).
structure eightdfs = dfs(eightqueens).